package com.ahu.qifei.fund.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ShiQifei
 * @date 2020/11/21 11:56
 */
@Slf4j
public class MailTemplate {
    private static final String MAIL_TEMPLATE_URL = "html/mailTemplate.html";
    private StringBuffer template;
    private List<StringBuffer> table;
    private StringBuffer tr;
    public MailTemplate() {
        this.template = new StringBuffer();
        this.table = new ArrayList<>();
        this.tr = new StringBuffer();
        template();
    }
    /**
     * 获取模板内容
     * @return
     * @throws IOException
     */
    public MailTemplate template()  {
        ClassPathResource cpr = new ClassPathResource(MAIL_TEMPLATE_URL);
        try {
            byte[] data = FileCopyUtils.copyToByteArray(cpr.getInputStream());
            this.template.append(new String(data));
        } catch (IOException e) {
            log.error("获取模板内容异常， {}，{}", e.getMessage(), e);
            throw new RuntimeException("获取模板内容异常，" + e.getMessage());
        }
        return this;
    }

    public MailTemplate code(String fundCode, String fundName) {
        this.tr.append("<td style=\" overflow: visible;\n" +
                "                    padding: .5em 1em;\n" +
                "                    border-bottom: 1px solid #cbcbcb;" +
                "                    padding: 10px;\n" +
                "                    width: 50px;\"><span style=\"color:blue\">")
                .append(fundCode).append("\n")
                .append(fundName)
                .append("</span></td>\n");
        return this;
    }

    public MailTemplate name(String fundName) {
        this.tr.append("<td style=\" overflow: visible;\n" +
                "                    padding: .5em 1em;\n" +
                "                    border-bottom: 1px solid #cbcbcb;" +
                "                    padding: 10px;\n" +
                "                    width: 50px;\">").append(fundName).append("</td>\n");
        return this;
    }

    public MailTemplate value(String fundValue, String betweenDays) {
        Double value = Double.valueOf(fundValue);
        String color = "gray";
        if(value > 0) {
            color = "red";
        } else if(value < 0) {
            color = "green";
        }
        this.tr.append("<td style=\" overflow: visible;\n" +
                "                    padding: .5em 1em;\n" +
                "                    border-bottom: 1px solid #cbcbcb;" +
                "                    padding: 10px;\n" +
                "                    width: 50px;\"><span style=\"color:")
                .append(color)
                .append(";font-weight:bold\">")
                .append(value)
                .append("%</span> (")
                .append(betweenDays)
                .append(")</td>\n");
        return this;
    }

    public MailTemplate addTr() {
        this.tr.insert(0, "<tr>\n").append("</tr>\n");
        this.table.add(this.tr);
        this.tr = new StringBuffer();
        return this;
    }

    @Override
    public String toString() {
        for(StringBuffer tr : this.table) {
            this.template.append(tr);
        }
        close();
        return this.template.toString();
    }

    public MailTemplate close() {
        this.template.append("</tbody>\n" +
                "        </table>\n" +
                "    </body>\n" +
                "\n" +
                "</html>");
        return this;
    }
}
